package com.android.qjpmc.data_module;

import com.android.qjpmc.bean.PwrDeviceBean;

import java.util.ArrayList;
import java.util.List;

/*
 * 设备集合，
 * 双向链表
 *
 *
 *
 * */

public class PwrDeviceSet {
    PwrDevice m_Head = null;

    PwrDevice GetHead()
    {
        return m_Head;
    }

    void SetHead(PwrDevice Device)
    {
        m_Head = Device;
    }

    PwrDevice GetTail()
    {
        PwrDevice Head = GetHead();
        PwrDevice Next = null;
        if (Head == null)
        {
            return null;
        }
        Next = Head.GetNext();
        while (Next != null)
        {
            Head = Next;
            Next = Head.GetNext();
        }
        return Head;
    }
    public int AppendADevice(PwrDevice Device)
    {
        PwrDevice Tail = GetTail();
        if (Tail == null)
        {
            SetHead(Device);
        }
        else
        {
            Tail.AppendANode(Device);
        }
        return 0;
    }
    public PwrDevice GetPwrDevice(int ID)
    {
        PwrDevice Device = GetHead();
        while(Device != null)
        {
            if (Device.IsMe(ID) == 1)
            {
                return  Device;
            }
            Device = Device.GetNext();
        }
        return Device;
    }
    public PwrDevice GetPwrDeviceByIndex(int index)
    {
        PwrDevice Device = GetHead();
        int the_idx = 0;
        while(Device != null)
        {
            if (the_idx == index)
            {
                return Device;
            }
            the_idx++;
            Device = Device.GetNext();
        }
        return Device;
    }
    //移除设备节点
    int RemoveDevice(int DeviceID)
    {
        PwrDevice Head = GetHead();
        PwrDevice PreNode = null;
        PwrDevice NextNode = null;
        PwrDevice TheNode = null;
        TheNode = Head;
        while(TheNode != null)
        {
            if (TheNode.IsMe(DeviceID) == 1)
            {
                PreNode = TheNode.GetNext();
                NextNode = TheNode.GetPre();
                if (TheNode == Head)
                {
                    SetHead(NextNode);
                    TheNode = null;
                }
                else
                {

                    if (PreNode != null)
                    {
                        PreNode.SetNext(NextNode);
                    }
                    if (NextNode != null)
                    {
                        NextNode.SetPre(PreNode);
                    }
                }
                return 0;

            }
            TheNode = TheNode.GetNext();
        }
        return 1;
    }
    //释放设备链表
    public int Release()
    {
        PwrDevice Head = GetHead();
        PwrDevice TheNode = null;
        PwrDevice ToDelNode = null;
        TheNode = Head;
        while(TheNode != null)
        {
            ToDelNode = TheNode;
            TheNode = TheNode.GetNext();
            ToDelNode = null;
        }
        return 1;
    }
    //析构函数
    public void finalize()
    {
        Release();
    }

    //获取设备beans
    public List<PwrDeviceBean> GetDeviceBeanList()
    {
        List<PwrDeviceBean> BeanList =  new ArrayList<PwrDeviceBean>();
        PwrDevice TheNode = GetHead();
        while(TheNode != null)
        {
            BeanList.add(TheNode.GetDeviceBean()) ;
            TheNode = TheNode.GetNext();
        }
        return BeanList;
    }
    //根据GroupBean创建设备
    public PwrDevice CreatePowerDevice(PwrDeviceBean Bean)
    {
        if (Bean == null)
        {
            return null;
        }
        PwrDevice NewDevice = new PwrDevice();
        if (NewDevice == null)
        {
            return null;
        }
        NewDevice.SetDeviceBean(Bean);
        AppendADevice(NewDevice);
        return  NewDevice;
    }

    public int CreateAllPowerDeviceOfAStation(List<PwrDeviceBean> Beans)
    {
        int DeviceNum = 0;
        int FailedNum = 0;
        PwrDevice Device = null;
        for (PwrDeviceBean Bean:  Beans) {
            Device = CreatePowerDevice(Bean);
            if (Device != null)
            {
                DeviceNum++;
            }
            else
            {
                FailedNum++;
            }
        }
        return DeviceNum;
    }
    public int GetCount()
    {
        int Count = 0;
        PwrDevice Head = GetHead();
        while(Head != null)
        {
            Count++;
            Head = Head.GetNext();
        }
        return Count;
    }

    public int IsLastDevice(PwrDevice Device)
    {
        if (Device == null)
        {
            return 0;
        }
        if (GetTail() == Device)
        {
            return 1;
        }
        return 0;
    }

}


